package com.fan.shop.businesslogic;

import org.apache.log4j.Logger;

import com.fan.shop.domain.Category;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.CategoryService;

public class CategoryDeleteProcess extends ProcExec<Integer> {

	private static Logger logger = Logger.getLogger(CategoryDeleteProcess.class);

	private String name;

	private CategoryService categoryService;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public CategoryDeleteProcess() {

	}

	@Override
	public Integer processImpl() {
		if (categoryService.getCategory(name) == null) {
			logger.debug("Category: " + name + " doesn't exist!");
			return ShopGlobalVariables.FAILED;
		}

		if (categoryService.isParent(categoryService.getCategory(name))) {
			logger.debug("Category: " + name
					+ " has sub categories, cannot be delete!");
			return ShopGlobalVariables.FAILED;
		} else {
			logger.debug("Category: " + name + " is about to be delete!");
			Category category = categoryService.getCategory(name);
			categoryService.deleteCategory(category);
			logger.debug("Category: " + name + " is delete!");
			return ShopGlobalVariables.SUCCESS;
		}

	}

}
